package com.nf.dao;

import com.nf.entity.Test;
import util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TestDaoImpl implements TestDao{
    public List<Test> getAll() {
        List<Test> testList = new ArrayList<Test>();

        try {
            Connection conn = DBUtil.getConnection();
            String sql = "select * from laowang";
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            while(rs.next()){

                Test lw = new Test();
                lw.setId( rs.getInt("id")  );
                lw.setName( rs.getString("name")  );
                lw.setAddr( rs.getString("addr")  );
                testList.add(lw);
            }
            rs.close();
            pst.close();
            conn.close();
            return testList;
        } catch (Exception e) {
            System.out.println("查询数据时发生异常："+e.getMessage());
            return null;
        }

    }

    public boolean deleteOne(int id) {

        try {
            Connection conn = DBUtil.getConnection();
            String sql = "delete from laowang where id =?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.execute();

            ps.close();
            conn.close();
            return true;
        } catch (Exception e) {
            System.out.println("删除数据错误"+e.getMessage());
            return false;
        }

    }

    public boolean addOne(Test t) {
        try {
            Connection conn = DBUtil.getConnection();
            String sql ="INSERT  INTO  laowang(name,addr) VALUES (?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,t.getName());
            ps.setString(2,t.getAddr());
            ps.execute();

            ps.close();
            conn.close();
            return true;
        } catch (Exception e) {
            System.out.println("添加数据错误"+e.getMessage());
            return false;
        }
    }

    public Test queryOneById(int id) {
        try {
            Connection conn = DBUtil.getConnection();
            String sql = "select * from laowang where id =?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            Test t = null;
            if (rs.next()){
                t = new Test();
                t.setId( rs.getInt("id")  );
                t.setName( rs.getString("name")  );
                t.setAddr( rs.getString("addr")  );

                rs.close();
                ps.close();
                conn.close();
                return t;
            }else{
                rs.close();
                ps.close();
                conn.close();
                return null;
            }

        } catch (Exception e) {
            System.out.println("通过id查询数据时发生异常："+e.getMessage());
            return null;
        }
    }

    public boolean updateOne(Test t) {
        try {
            Connection conn = DBUtil.getConnection();
            String sql = "update laowang set name=?,addr=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,t.getName());
            ps.setString(2,t.getAddr());
            ps.setInt(3,t.getId());
            ps.execute();

            ps.close();
            conn.close();
            return true;
        } catch (Exception e) {
            System.out.println("修改数据错误"+e.getMessage());
            return false;
        }
    }
}
